// build using command: go build -o go-walk
package main

import (
	_ "fmt"
	"os"
	"path/filepath"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/muesli/termenv"
)

func main() {
	startPath, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	argsWithoutFlags := make([]string, 0)

	for i := 1; i < len(os.Args); i++ {
		if os.Args[i] == "--help" || os.Args[1] == "-h" {
			usage()
		}
		if os.Args[i] == "--version" || os.Args[1] == "-v" {
			app_version()
		}
		if os.Args[i] == "--icons" {
			showIcons = true
			parseIcons()
			continue
		}
		if os.Args[i] == "--dir-only" {
			dirOnly = true
			continue
		}
		if os.Args[i] == "--preview" {
			startPreviewMode = true
			continue
		}
		if os.Args[i] == "--fuzzy" {
			fuzzyByDefault = true
			continue
		}
		argsWithoutFlags = append(argsWithoutFlags, os.Args[i])
	}

	if len(argsWithoutFlags) > 0 {
		startPath, err = filepath.Abs(argsWithoutFlags[0])
		if err != nil {
			panic(err)
		}
	}

	output := termenv.NewOutput(os.Stderr)
	lipgloss.SetColorProfile(output.ColorProfile())

	wm := &walker_model{
		path:        startPath,
		width:       app_run_init_width,
		height:      app_run_init_height,
		positions:   make(map[string]position),
		previewMode: startPreviewMode,
	}
	wm.list()

	p := tea.NewProgram(wm, tea.WithOutput(os.Stderr))
	if _, err := p.Run(); err != nil {
		panic(err)
	}
	os.Exit(wm.exitCode)
}
